package com.example.interrupt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class WaitDemo {
    private static final Logger log = LoggerFactory.getLogger(WaitDemo.class);
    public static final Object lock = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                log.info("进入了 t1");
                try {
                    TimeUnit.SECONDS.sleep(10);
                    lock.wait();
                    log.info("t1被唤醒了");
                } catch (InterruptedException e) {
                    log.error("t1 被打断了啊", e);
                }
            }
        }, "t1");


        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                log.info("t2进入了");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    log.error("error", e);
                }
                t1.interrupt();
                //lock.notifyAll();
            }
        }, "t2");

        t1.start();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            log.info("hello", e);
        }
        t2.start();
    }
}
